//
//  MovieDetail.m
//  MINFOBBS
//
//  Created by juvid on 13-12-2.
//  Copyright (c) 2013年 minfo. All rights reserved.
//

#import "MovieDetail.h"
#import "DetectionSystemVersion.h"
#import "UIColor+expanded.h"
#import "MainSearchViewController.h"
#import "MovieSong.h"
#import "MovieRole.h"
#import "MovieAllDetail.h"

@interface MovieDetail ()

@end

@implementation MovieDetail

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    if ([[DetectionSystemVersion alloc]Version]) {
        self.edgesForExtendedLayout=UIRectEdgeNone;
    }
    if (self.isSelf) {
        [self ShowData];
    }
    else{
        [self BackButton];
        self.vieMore.hidden=YES;
        self.vieOut.hidden=YES;
        [self.vieOut addSubview:self.vieHead];
        NSString *str=[[[[[[[self.arrReturn  objectForKey:@"Items"] objectForKey:@"Item"] objectAtIndex:0]objectForKey:@"Properties"] objectForKey:@"Property"] objectAtIndex:0] objectForKey:@"text"];
       
        self.labContent.text=str;
        CGSize strSize=[DetectionSystemVersion String:str Width:290 Font:15];
        self.labContent.frame=CGRectMake(10, self.vieHead.frame.size.height+10, 290, strSize.height);
        self.vieHead.frame=CGRectMake(0, 0, 310, self.vieHead.frame.size.height);
        self.vieBtn.frame=CGRectMake(0, self.labContent.frame.size.height+self.labContent.frame.origin.y+8, 310, self.vieHead.frame.size.height);
        self.vieOut.layer.cornerRadius=8;
        [self.vieOut.layer setMasksToBounds:YES];
//        str=[self.arrReturn objectForKey:@"Title"];
//        strSize=[DetectionSystemVersion String:str Width:20 Font:15];
        self.labTitle.text=[[[[self.arrReturn  objectForKey:@"Items"] objectForKey:@"Item"] objectAtIndex:0] objectForKey:@"Title"];
        

    }
    if (!self.isMovie) {
        arrQuery=[[self.arrReturn  objectForKey:@"Links"] objectForKey:@"Link"];
        if (![[[arrQuery objectAtIndex:1] objectForKey:@"text"]isEqual:@"下一集"]) {
            if (![[[arrQuery objectAtIndex:0] objectForKey:@"text"]isEqual:@"上一集"]) {
                [self.btnNext setTitle:@"第一集剧情" forState:UIControlStateNormal];
            }
            else{
                [self.btnNext setTitle:@"上一集剧情" forState:UIControlStateNormal];
            }
        }
        for (int i=0; i<arrQuery.count; i++) {
            if ([[[arrQuery objectAtIndex:i] objectForKey:@"text"] isEqual:@"影视歌词"]) {
                self.btnSong.hidden=NO;
            }
        }

    }
    else{
        self.vieBtn.hidden=YES;
    }
        // Do any additional setup after loading the view from its nib.
}
-(void)viewDidAppear:(BOOL)animated{
    if (!self.isSelf) {
        self.vieOut.hidden=NO;
        if (self.isMovie) {
             self.vieOut.frame=CGRectMake(5, 5, self.view.frame.size.width-10,self.vieBtn.frame.size.height+self.vieBtn.frame.origin.y-20);
        }
        else{
             self.vieOut.frame=CGRectMake(5, 5, self.view.frame.size.width-10,self.vieBtn.frame.size.height+self.vieBtn.frame.origin.y+10);
        }
    }
}
-(void)ShowData{
    self.labContent.text=[[[[[[[self.arrReturn  objectForKey:@"Items"] objectForKey:@"Item"] objectAtIndex:0] objectForKey:@"Properties"] objectForKey:@"Property"] objectAtIndex:0] objectForKey:@"text"];
}
-(void)BackButton{
    self.navigationItem.titleView=[DetectionSystemVersion SetBarTitle:@"百事搜"];
    [self.navigationItem setHidesBackButton:YES];
    UIButton *btnBack=[[DetectionSystemVersion alloc]BackButton];
    [btnBack addTarget:self action:@selector(BackReturn:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc]initWithCustomView:btnBack];
    self.navigationItem.leftBarButtonItem=backItem;
    
}
#pragma mark 导航栏按钮事件
-(void)BackReturn:(id)sender{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (MainSearchViewController *)menuContainerViewController {
    return (MainSearchViewController *)self.parentViewController;
}
- (IBAction)PressMore:(id)sender {
    MovieDetail *jokecon=[[MovieDetail alloc]init];
        jokecon.isMovie=self.isMovie;
    jokecon.arrReturn=self.arrReturn;
    [ [self menuContainerViewController].navigationController  pushViewController:jokecon animated:YES];
}

- (IBAction)PressRole:(id)sender {
    MovieRole *jokecon = [[MovieRole alloc] init];
     jokecon.strKey= [self  GetQuery:@"角色介绍"];
    if (self.isSelf) {
        [ [self menuContainerViewController].navigationController  pushViewController:jokecon animated:YES];
    }
    else{
        
        [self.navigationController pushViewController:jokecon animated:YES];
    }

}

- (IBAction)PressSong:(id)sender {
    MovieSong *jokecon = [[MovieSong alloc] init];
    jokecon.strKey= [self  GetQuery:@"影视歌词"];

    if (self.isSelf) {
       [ [self menuContainerViewController].navigationController  pushViewController:jokecon animated:YES];
    }
    else{
        
         [self.navigationController pushViewController:jokecon animated:YES];
    }

}

- (IBAction)PressNext:(id)sender {
    MovieAllDetail *jokecon = [[MovieAllDetail alloc] init];
    if (![[[arrQuery objectAtIndex:1] objectForKey:@"text"]isEqual:@"下一集"]) {
         jokecon.strKey= [self  GetQuery:[[arrQuery objectAtIndex:0] objectForKey:@"text"]];
    }
    else{
     jokecon.strKey= [self  GetQuery:@"下一集"];
    }
    if (self.btnSong.hidden) {
        jokecon.isSong=YES;
    }
    if (self.isSelf) {
        [ [self menuContainerViewController].navigationController  pushViewController:jokecon animated:YES];
    }
    else{
        
        [self.navigationController pushViewController:jokecon animated:YES];
    }

}
-(NSString *)GetQuery:(NSString *)query{
    NSString *str;
    for (int i=0; i<arrQuery.count; i++) {
        if ([[[arrQuery objectAtIndex:i] objectForKey:@"text"] isEqual:query]) {
             str=[[arrQuery objectAtIndex:i] objectForKey:@"href"];
            NSRange range = [str rangeOfString:@"query="];
            str=[str substringFromIndex:range.location +range.length];
        }
    }
    //
    return str;
    
}
@end
